/**
 * @description 数据库用户表操作
 * @author 盘挂他爹
 */


import { IUserInfo, IUpdateUserInfo } from "./types"
import { User } from "../../model/user"
import { DEFAULT_AVATAR } from "../../config/constant"

class UserService {

    /**
     * 获取用户基本信息
     * @param userInfo 
     */
    async getUserInfo(userInfo: IUserInfo) {
        const { userName } = userInfo
        console.log("userName2", userName);

        //查询用户是否存在
        try {
            const res = await User.findOne({
                where: {
                    userName
                }
            })

            console.log("serviceDe1res", res);
            //用户头像为空的补全操作
            if (res) {

                if (!res.dataValues.avatar) {
                    res.dataValues.avatar = DEFAULT_AVATAR
                }
                console.log("没有头像发", DEFAULT_AVATAR);
                return res.dataValues
            }
            return res
        } catch (err) {
            console.log("err:", err);

        }



    }


    async registerUser(userInfo: IUserInfo) {
        const res = await User.create(userInfo)
        if (res) {
            return res.dataValues
        }
        return res
    }

    async checkUserByPassword(password: string) {
        //密码比较
        const res = await User.findOne({
            where: {
                password
            }
        })
        console.log("查询结果", res);

        return res
    }

    async updateUserInfo(userInfo: IUpdateUserInfo, id: number) {
        const res = await User.update({ ...userInfo }, {
            where: {
                id
            }
        })
        console.log("更新结果", res);
        return res[0] > 0 ? true : false



    }

}

export default new UserService()